package com.net.test.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.net.test.service.LolService;
import com.net.test.vo.LolVO;


@Controller
public class LolController {
	private static final Logger logger = LoggerFactory.getLogger(LolService.class);
	@Autowired
	LolService lolService;
	@RequestMapping(value = "/lolList.ncb", method = {RequestMethod.GET,RequestMethod.POST})
    public String LolList(Locale locale, Model model) throws Exception {
        logger.info("Welcome home! The client locale is {}.", locale); 
        
        List<LolVO> list = (List<LolVO>)lolService.selectData("testMapper.selectLol");
        
        model.addAttribute("list" , list);
        return "lolList";        
        
	}
	
	@RequestMapping(value = "/lolWrite.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolWrite(Locale locale , Model model) throws Exception{
		logger.info("Welcome home! The client locale is {}.", locale); 
		return "lolWrite";
	}
	
	
	@RequestMapping(value = "/lolInsert.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolWrite(Locale locale , Model model, HttpServletRequest request) throws Exception{
		
		LolVO insert = new LolVO();
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		
		insert.setBoard_Title(title);
		insert.setBoard_Contents(contents);
		
		int result = lolService.insertData("testMapper.lolInsert", insert);
		
		List<LolVO> list = (List<LolVO>)lolService.selectData("testMapper.selectLol");
		
		model.addAttribute("list",list);
		
		return "lolList";		
		
	}
	
	@RequestMapping(value = "/lolView.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolView(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		
		String title = request.getParameter("title");
			
		LolVO view = (LolVO)lolService.ViewData("testMapper.viewLol",title);
		
		model.addAttribute("view", view);
		
		return "lolView";		
		
	}
	
	@RequestMapping(value ="/lolEdit.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolEdit(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("edit1");
		String contents = request.getParameter("edit2");
		
		LolVO edit = new LolVO();
		
		edit.setBoard_Title(title);
		edit.setBoard_Contents(contents);
		
		model.addAttribute("edit", edit);
		
		return "lolEdit";
	}
	@RequestMapping(value ="/lolUpdate.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolUpdate(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		String pretitle = request.getParameter("pretitle");
		
		LolVO update = new LolVO();
		
		update.setBoard_Title(title);
		update.setBoard_Contents(contents);
		update.setBoard_Name(pretitle);
		
		int result = lolService.updateData("testMapper.updateLol", update);
		
		List<LolVO> list = (List<LolVO>)lolService.selectData("testMapper.selectLol");
		
		model.addAttribute("list", list);
		
		return "lolList";		
		
	}
	@RequestMapping(value ="/lolDelete.ncb", method = {RequestMethod.GET, RequestMethod.POST})
	public String LolDelete(Locale locale, Model model, HttpServletRequest request)throws Exception{
		
		
		LolVO delete = new LolVO();
		
		String title = request.getParameter("del");		
		
		delete.setBoard_Title(title);
		
		
		int result = lolService.deleteData("testMapper.lolDelete", delete);
		
		List<LolVO> list = (List<LolVO>)lolService.selectData("testMapper.selectLol");
		
		model.addAttribute("list", list);
		
		return "lolList";
		
	}
	
}
